package BluetoothFileMgr.start;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class ConnectThread implements Runnable{
	
	    
	    //private final BluetoothDevice mmDevice;
	    //private final BluetoothAdapter adapter;
	    private final Context context;
	    DataInputStream din;	    
	    
	    public ConnectThread(BluetoothDevice device, BluetoothAdapter adapter, Context context, UUID MY_UUID, BluetoothSocket  mmSocket) {
	        // Use a temporary object that is later assigned to mmSocket,
	        // because mmSocket is final
	        //BluetoothSocket tmp = null;
	        //mmDevice = device;
	        //this.adapter = adapter;
	        this.context = context;
	        din = DeviceConnect.din;
	        //this.mmSocket = mmSocket;
	 
	        /*// Get a BluetoothSocket to connect with the given BluetoothDevice
	        try {
	            // MY_UUID is the app's UUID string, also used by the server code
	            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
	        } catch (IOException e) { }
	        mmSocket = tmp;*/
	    }
	    
	    public void empty()
	    {
	    	
	    }
	    
	    Handler hand = new Handler()
        {  	public void handleMessage(Message msg)
        	{
        		if((msg.obj).toString().contains("-->"))
        		{
        			Toast toast = Toast.makeText(context, (msg.obj).toString(), Toast.LENGTH_SHORT);
        			toast.show();
        		}
        		if((msg.obj).toString().contains("!"))
        		{
        			String[] item = new String[0];
        			item = (msg.obj).toString().split("!");
        			if(item[0].equals("empty"))
        			{
        				
        			}
        			else
        			{
        				FileManager.dirs.add(item[1]);
        				FileManager.type.add(item[0]);
        				FileManager.size.add(Long.parseLong(item[2]));
        			}
        			FileManager.adapter2.notifyDataSetChanged();
        		}
        		else if((msg.obj).toString().contains("&&&"))
        		{
        			String s = (msg.obj).toString();
        			String[] split = s.split("&&&");
        			Toast toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
        			String s1 = split[0];
        			FileManager.tv2.setText(s1);//(msg.obj).toString().split("*#*")[0]);
        		}
        		else
        		{
        			Toast toast = Toast.makeText(context, (msg.obj).toString(), Toast.LENGTH_SHORT);
        			//toast.show();
        		}
        	}
        };	
	 
	    public void run()
	    {
	        //BluetoothFileMgrActivity.adap.cancelDiscovery();
	        try
	        {
	            try
    	    	{	Message msg = hand.obtainMessage();
    	    		msg.obj = "before connect";
    	    		//hand.sendMessage(msg);
    	    	} catch (Exception e)
    	    	{	Message msg = hand.obtainMessage();
    	    		msg.obj = "in catch of before connect msg";
    	    		//hand.sendMessage(msg);
    	    	}
	        	//InputStream in = DeviceConnect.mmSocket.getInputStream();
	        	//Scanner sc = new Scanner(in);
	        
	            
	            int i = 0;
	            
	            String st = din.readUTF();
	            
	            Runnable writeThread = new WriteThread("DOWNLOAD!!"+"RENAME.jpg");
	    		Thread t3 = new Thread(writeThread);
	    		t3.start();
	            
	            //Runnable fileSocket = new FileSocket("RENAME.jpg", DeviceConnect.device, 2486, dc);
	    		//Thread t9 = new Thread(fileSocket);
	    		//t9.start();
	            
	           /* while(true)
	            ///////////////////////////////////////////////////////////////
	            {
	            	//String st = sc.nextLine();
	            	String st = din.readUTF();
	            	if(!st.contains("!!"))
	            	try
            		{	Message msg = hand.obtainMessage();
            			msg.obj = i+"-->"+st;
            			hand.sendMessage(msg);
            		}
            		catch (Exception e)
            		{	Message msg = hand.obtainMessage();
            			msg.obj = "in catch of bytes";
            			hand.sendMessage(msg);
            		}
	            	String[] str = new String[10];
	            	FileManager.dirs.clear();
	            	FileManager.type.clear();
	            	FileManager.size.clear();
	            	str = st.split("!!");
	            	if(st.contains("!!"))
	            		for(int j = 0; j<str.length; j++)
	            		{
	            			try
	            			{	Message msg = hand.obtainMessage();
	            				if(j==0)
	            					msg.obj=str[j]+"&&&";
	            				else
	            					msg.obj = str[j];
	            				hand.sendMessage(msg);
	            			}
	            			catch (Exception e)
	            			{	Message msg = hand.obtainMessage();
	            				msg.obj = "in catch of bytes";
	            				hand.sendMessage(msg);
	            			}
	            		}
	            	i++;
	            }*/
	            ///////////////////////////////////////////////////////////////////
	                        
	        } catch (IOException connectException) {
	            // Unable to connect; close the socket and get out
	            try {
	                //mmSocket.close();
	            	 try
	     	    	{	Message msg = hand.obtainMessage();
	     	    		msg.obj = "conn failed";
	     	    		//hand.sendMessage(msg);
	     	    	} catch (Exception e)
	     	    	{	Message msg = hand.obtainMessage();
	     	    		msg.obj = "in catch of con failed";
	     	    		//hand.sendMessage(msg);
	     	    	}
	            } catch (Exception closeException) {
	            	try
	     	    	{	Message msg = hand.obtainMessage();
	     	    		msg.obj = "outer catch";
	     	    		//hand.sendMessage(msg);
	     	    	} catch (Exception e)
	     	    	{	Message msg = hand.obtainMessage();
	     	    		msg.obj = "in catch of outer catch";
	     	    		//hand.sendMessage(msg);
	     	    	}
	            }
	            return;
	        }
	        // Do work to manage the connection (in a separate thread)
	        //manageConnectedSocket(mmSocket);
	    }
	 
	    /** Will cancel an in-progress connection, and close the socket */
	    public void cancel() {
	        try {
	            DeviceConnect.mmSocket.close();
	        } catch (IOException e) { }
	    }
	    
	}